package com.pervasive.geoTagging;

import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

public class PreferencesActivity extends PreferenceActivity{

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preference);
        edit = MainActivity.pref.edit();
        
		list = (ListPreference)findPreference("marker");
		list.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			public boolean onPreferenceChange(Preference arg0, Object arg1) {
				String marker = (String)arg1;
		        edit.putString(MARKER, marker).commit();
				return true;
			}
		});
        
        flash = (ListPreference)findPreference("flash");
        flash.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			public boolean onPreferenceChange(Preference p, Object o) {
				String str = (String)o;
		        edit.putString("Flash", str).commit();
				return true;
			}
		});
        
		quality = (ListPreference)findPreference("quality");
		quality.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			public boolean onPreferenceChange(Preference p, Object o) {
				String str = (String)o;
		        edit.putString("Quality", str).commit();
				return true;
			}
		});
        
        ip = (EditTextPreference)findPreference("server_ip");
        ip.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			public boolean onPreferenceClick(Preference preference) {
				String temp = MainActivity.pref.getString("IP", "localhost");
				ip.getEditText().setText(temp);
				return true;
			}
        });
        ip.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				String str = (String)newValue;
				if(str.matches("([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){1,3}")){
					edit.putString("IP", str).commit();
				}else{
					edit.putString("IP", "localhost").commit();
				}
				return true;
			}
        });
	}

	private ListPreference list, flash, quality;
	private EditTextPreference ip;
	private Editor edit;
	protected static final String MARKER = "marker";
}
